var express = require('express');
var router = express.Router();
var db = require('../db.js');
var color = require('../color.js');
/* GET users listing. */

//all
router.all('/all', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  db.User.find().then(function (data) {
    res.json({ data })
  })
})
//query
router.all('/query', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  var pageNum = reqData.pageNum*1 || 1;
  var pageSize = reqData.pageSize*1 || 20;
  if(!!reqData.query&&!!reqData.query.$reg){
    for (const key in reqData.query.$reg) {
      if (reqData.query.hasOwnProperty(key)) {
        reqData.query[key]=new RegExp(reqData.query[key],'i')    
      }
    }
    delete reqData.query.$reg;
  }
  db.User.find(reqData.query||{}).then(function (data) {
    var pageCount=Math.ceil(data.length/pageSize);
    pageNum=pageNum>pageCount?pageCount:pageNum;
    console.log('userjs 39',reqData.query)
    db.User.find(reqData.query||{}).sort({'_id':-1}).skip((pageNum==0?0:pageNum-1)*pageSize).limit(pageSize).then(function (data) {
      res.json({
        code:200,
        pageNum,
        pageSize,
        pageCount,
        data
      })
    })
  })
});
//add
router.all('/add', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  console.log(req.body)
  db.User.find({ name: reqData.query.name }).then(function (data) {
    if (data.length == 0) {
      req.body.headColor = Math.floor(Math.random() * color.length)
      var user = new db.User(reqData.query);
      user.save().then(function () {
        res.json({
          code: 200,
          msg: '注册成功'
        })
      })
    } else {
      res.json({
        code: 1001,
        msg: '该用户名已经被注册'
      })
      console.log(data)
    }
  })
});
//updata
router.all('/updata', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  console.log('userjs 72',reqData)
  
  db.User.findOneAndUpdate(reqData.query, reqData.data).then(function (data) {
    res.json({
      code: 200,
      msg: '提交成功',
      data
    })
  })
});
// login
router.all('/login', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  console.log(reqData.query)
  db.User.find(reqData.query).then(function (data) {
    console.log(data)
    if (data.length == 0) {
      res.json({
        code: 1004,
        msg: '该用户名不存在的Orz'
      })
    } else {
      console.dir(data[0].pwd)
      if (data[0].pwd == reqData.data.pwd) {
        res.cookie('user', data[0].name)
        res.json({
          code: 200,
          msg: '登录成功！',
          data
        })
      } else {
        res.json({
          code: 1001,
          msg: '用户名和密码对不上哦Orz'
        })
      }
    }
  });
})
// logout
router.all('/logout', function (req, res) {
  if (req.method == "POST") {
    var reqData = req.body;
  } else {
    var reqData = req.query || req.params;
  }
  res.clearCookie('user');
  res.redirect('/lnr.html')

});
module.exports = router;

